﻿<cfsilent>
	<cfsetting requesttimeout="90" />
	<cfscript>

		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		datetimeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");

		chooseProp = getProperty("serviceFactory").getBean("choosePropertyAdvice");
		taskProp = getProperty("serviceFactory").getBean("taskPropertyAdvice");
		turnProp = getProperty("serviceFactory").getBean("turnPropertyAdvice");
		studentPropAdvice = getProperty("serviceFactory").getBean("studentPropertyAdvice");
		
		sequenceStudentCourse = getProperty("serviceFactory").getBean("sequenceStudentCourse");

		termId = event.getArg("TermID");
		courseId = event.getArg("CID");
		courseIdx = event.getArg("CIDX");
		taskType = event.getArg("TT");
		taskId = event.getArg("TSK");

		coursePropId = event.getArg("CP");

		secureTicket = event.getArg("ST");
		
		turnId = event.getArg("CT");

		/* 学籍相关信息 */
		studentId = sessionAdvice.getAutherUserID();
		//studentClassId = sessionAdvice.getUserProp("studentClass");
		studentSubjectId = sessionAdvice.getUserProp("studentSubject");
		//studentDirId = sessionAdvice.getUserProp("studentSubDir");
		studentGrade = sessionAdvice.getUserProp("studentGrade");
		studentDepartment = sessionAdvice.getUserProp("studentDepartment");
		studentProps = sessionAdvice.getUserProp("studentProps");
		
		studentPropAdvice.parseProperty( studentProps );

		chooserSecure = false;
		chooserAllowed = false;

		/* 回执信息 */
		response = structNew();
		structInsert(response, "success", false, true);


		/* 首先做安全检查 */
		try {
			stringOrignal = decrypt(secureTicket, taskId, "PBEWithMD5AndDES", "Hex");
			stringTest = courseId & courseIdx & termId & taskType;
			
			if ( stringOrignal eq stringTest ) {
				chooserSecure = true;
			}
		}
		catch(any excpt) {
		}
		
		if ( chooserSecure ) {

			/* 检查选课轮次 */
			if ( len(turnId) ) {
				sql = "SELECT 
							date_start,date_end,time_start,time_end,
							grade_allowed,institute_allowed,subject_allowed,turn_prop 
						FROM 
							t_choose_turn 
						WHERE 
							ct = :turnId ";
				queryObj = new query( datasource=application.dnsSlave );
				queryObj.addParam( name="turnId", value=turnId, cfsqltype="cf_sql_char" );
				rs_turn = queryObj.execute( sql=sql ).getResult();
				
				if (rs_turn.recordCount gt 0) {
				
					today = datetimeAdvice.getCurrentDateString();
					currentTime = TimeFormat(now(), "HHMM");
				
					if ( rs_turn.date_start lte today and today lte rs_turn.date_end ) {
						if ( rs_turn.time_start lte currentTime and currentTime lte rs_turn.time_end ) {
							if ( rs_turn.grade_allowed eq "ALL" or listFind(rs_turn.grade_allowed, studentGrade, ",") ) {
								if ( rs_turn.institute_allowed eq "ALL" or listFind(rs_turn.institute_allowed, studentDepartment, ",") or (rs_turn.subject_allowed neq '-' and listFind(rs_turn.subject_allowed, studentSubjectId)) ) {
									/* 是否开放双学位选课 */
									turnProp.parseProperty(rs_turn.turn_prop);
									if ( turnProp.getSecondary() and studentPropAdvice.getSecondary() ) {
										chooserAllowed = true;
									}
								}
							}
						}
					}
				}
			}
			
			if ( chooserAllowed ) {
				
				transaction {
	
					  /* 添加选课记录 */
					  switch(taskType){
						  case 'C':
							  sql = "SELECT 
									 	tsk_id, task_limite, task_chooesd 
									 FROM 
									 	t_common_task  
									 WHERE 
										tsk_id = :taskId ";
							  break;
						  case 'P':
							  sql = "SELECT 
									  	tsk_id, task_limite, task_chooesd  
									 FROM 
									 	t_task tsk  
									 WHERE 
										tsk.tsk_id = :taskId ";
							  break;
						  case 'R':
							  sql = "SELECT 
									 	tsk_id, task_limite, task_chooesd 
									 FROM 
									 	t_reopen_task tsk  
									 WHERE 
										tsk.tsk_id = :taskId ";
							  break;
						  case 'T':
							  sql = "SELECT 
									 	tsk_id, task_limite, task_chooesd  
									 FROM 
									 	t_sport_task tsk  
									 WHERE 
										tsk_id = :taskId ";
							  break;
					  }
					  queryObj = new Query(datasource = application.dnsMaster);
					  queryObj.addParam(name="taskId",value=taskId, cfsqltype="cf_sql_varchar");
					  rs_task = queryObj.execute(sql=sql).getResult();
					  
					  /* 检查教学班是否还有空席位 */
					  if ( rs_task.task_limite gt rs_task.task_chooesd ) {
						  
						  switch(taskType){
							  case 'C':
								  sql = "UPDATE t_common_task 
										SET task_chooesd = task_chooesd + 1 
										WHERE tsk_id = :taskId ";
								  break;
							  case 'P':
							 	  sql = "UPDATE t_task 
										SET task_chooesd = task_chooesd + 1 
										WHERE tsk_id = :taskId ";
								  break;
							  case 'R':
							  	  sql = "UPDATE t_reopen_task  
										SET task_chooesd = task_chooesd + 1 
										WHERE tsk_id = :taskId "; 
								  break;
							  case 'T':
							  	  sql = "UPDATE t_sport_task 
										SET task_chooesd = task_chooesd + 1 
										WHERE tsk_id = :taskId ";
								  break;
						  }
						  queryObj = new query( datasource=application.dnsMaster );
						  queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
						  queryObj.execute( sql=sql );
						  
						  sequenceStudentCourse.setStudentID(studentId);
						  sequenceStudentCourse.setCourseId(courseId);
						  sequenceStudentCourse.setTermID(termId);
						  sequenceStudentCourse.setTaskType(taskType);
						  
						  chooserId = sequenceStudentCourse.getID();
					  
						  timeStamp = datetimeAdvice.getCurrentDatetimeString();
						  
						  sql = " SELECT
									  scc_id, task_id, task_type, choose_prop 
								  FROM
									  t_student_course
								  WHERE
									  scc_id = :chooserId ";
						  queryObj = new Query(datasource = application.dnsMaster);
						  queryObj.addParam(name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar");
						  rs_student_course = queryObj.execute(sql = sql).getResult();
						  
						  if ( rs_student_course.recordCount EQ 0 ) {
							  
							  prop = chooseProp.init();
							  
							  /* 预置课表是否锁定 */
							  prop.setLocked(false);
							  /* 缴费状态设置 */
							  prop.setPrePaid( false ); /* 是否从预交学费扣除 */
							  prop.setPaid( false ); /* 是否已收费 */
  
							  /* 没有选课记录 */
							  sql = "INSERT INTO t_student_course
												(scc_id,
												 task_type,
												 cidx,
												 task_id,
												 st,
												 retaking,
												 choose_time,
												 charge_time,
												 choose_prop,
												 term_id,
												 cid,
												 ct,
												 stu_id,
												 prop_id)
											  VALUES
											   ( :chooserId ,
												 :taskType ,
												 :courseIdx ,
												 :taskId ,
												 :studyType ,
												 :retaking ,
												 :chooseTime ,
												 :chargeTime ,
												 :chooseProp ,
												 :termId ,
												 :courseId ,
												 :turnId ,
												 :studentId ,
												 :propId )";
							  queryObj = new Query(datasource = application.dnsMaster);
							  queryObj.addParam(name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar");
							  queryObj.addParam(name="taskType", value=taskType, cfsqltype="cf_sql_varchar");
							  queryObj.addParam(name="courseIdx", value=courseIdx, cfsqltype="cf_sql_varchar");
							  queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_char");
							  queryObj.addParam(name="studyType", value='S', cfsqltype="cf_sql_char");
							  queryObj.addParam(name="retaking", value='0', cfsqltype="cf_sql_char");
							  queryObj.addParam(name="chooseTime", value=timeStamp, cfsqltype="cf_sql_char");
							  queryObj.addParam(name="chargeTime", value=timeStamp, cfsqltype="cf_sql_varchar");
							  queryObj.addParam(name="chooseProp", value=prop.stringProperty(), cfsqltype="cf_sql_char");
							  queryObj.addParam(name="termId", value=termId, cfsqltype="cf_sql_char");
							  queryObj.addParam(name="courseId", value=courseId, cfsqltype="cf_sql_varchar");
							  queryObj.addParam(name="turnId", value=turnId, cfsqltype="cf_sql_char");
							  queryObj.addParam(name="studentId", value=studentId, cfsqltype="cf_sql_varchar");
							  queryObj.addParam(name="propId", value=coursePropId, cfsqltype="cf_sql_varchar");
							  queryObj.execute(sql = sql);
					  
						  }
						  else {
  
							  /* 更新选课记录 */
							  prop = chooseProp.init();
							  prop.parseProperty( rs_student_course.choose_prop );
							  /* 缴费状态设置 */
							  prop.setPrePaid( false ); /* 是否从预交学费扣除 */
							  prop.setPaid( false ); /* 是否已收费 */
  
							  //改选以后，将原来的教学任务人数实际人数减去1
							  switch(rs_student_course.task_type){
								  case 'P':
									  sql = "UPDATE t_task
											SET task_chooesd = task_chooesd - 1 
											 WHERE tsk_id = :taskId ";
									  
									  updateObj = new Query(datasource=application.dnsMaster);
									  updateObj.addParam(name="taskId", value=rs_student_course.task_id, cfsqltype="cf_sql_varchar");
									  updateObj.execute(sql=sql);
									  break;
								  case 'C':
									  sql = "UPDATE t_common_task
											 SET task_chooesd = task_chooesd - 1 
											 WHERE tsk_id = :taskId ";
									  
									  updateObj = new Query(datasource=application.dnsMaster);
									  updateObj.addParam(name="taskId", value=rs_student_course.task_id, cfsqltype="cf_sql_varchar");
									  updateObj.execute(sql=sql);
									  break;
								  case 'R':
									  sql = "UPDATE t_reopen_task
											SET task_chooesd = task_chooesd - 1 
											 WHERE tsk_id = :taskId ";
									  
									  updateObj = new Query(datasource=application.dnsMaster);
									  updateObj.addParam(name="taskId", value=rs_student_course.task_id, cfsqltype="cf_sql_varchar");
									  updateObj.execute(sql=sql);
									  break;
								  case 'T':
									  sql = "UPDATE t_sport_task
											 SET task_chooesd = task_chooesd - 1 
											 WHERE tsk_id = :taskId ";
									  
									  updateObj = new Query(datasource=application.dnsMaster);
									  updateObj.addParam(name="taskId", value=rs_student_course.task_id, cfsqltype="cf_sql_varchar");
									  updateObj.execute(sql=sql);
									  break;
							  }
							  /*Unique: STU_ID, CID, TERM_ID, TASK_TYPE*/
							  sql = "UPDATE t_student_course 
									 SET 
										  task_type = :taskType, 
										  choose_prop = :chooseProp ,
										  cidx = :courseIndex ,
										  task_id = :taskId ,
										  st = :studyType ,
										  retaking = :retaking ,
										  choose_time = :chooseTime ,
										  charge_time = :chargeTime ,
										  ct = :turnId ,
										  prop_id = :propId 
									  WHERE
										  scc_id = :chooserId ";
							  
							  queryObj = new Query(datasource = application.dnsMaster);
							  
							  queryObj.addParam(name="taskType", value=taskType, cfsqltype="cf_sql_char");
							  queryObj.addParam(name="chooseProp", value=prop.stringProperty(), cfsqltype="cf_sql_varchar");
							  queryObj.addParam(name="courseIndex", value=courseIdx, cfsqltype="cf_sql_char");
							  queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_varchar");
							  queryObj.addParam(name="studyType", value="S", cfsqltype="cf_sql_char");
							  queryObj.addParam(name="retaking", value="0", cfsqltype="cf_sql_char");
							  queryObj.addParam(name="chooseTime", value=timeStamp, cfsqltype="cf_sql_char");
							  queryObj.addParam(name="chargeTime", value=timeStamp, cfsqltype="cf_sql_char");
							  queryObj.addParam(name="turnId", value=turnId, cfsqltype="cf_sql_char");
							  queryObj.addParam(name="propId", value=coursePropId, cfsqltype="cf_sql_char");
							  queryObj.addParam(name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar");
							  
							  queryObj.execute( sql=sql );
							  
						  }

						  structInsert(response, "success", true, true);
						  
					  }
					  else {
						  structInsert(response, "reason", "选课人数已满", true);
					  }
				}

			}
			else {
				structInsert(response, "reason", "现在不是选课时段", true);
			}
		}
		else {
			structInsert(response, "reason", "参数错误", true);
		}

	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>